using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Audible
{
    class Verse
    {
        public string NameWav;
        public string ContentWav;
    }
    class Chapter
    {
        public List<Verse> Verses = new List<Verse>();
    }
    class Book
    {
        public string Title;
        public List<Chapter> Chapters = new List<Chapter>();
    }

    class BibleXml
    {
        private Book book = new Book();
        private int chapter = 1;
        private int verse = 1;
        public string Book
        {
            get { return book.Title; }
            set {
                // If book title does not match value then find the one that does
                if (book != null || book != null && book.Title != value)
                {
                    book = null;
                    // Find book with title that matches value
                    foreach (Book b in books)
                        if (b.Title == value)
                            book = b;
                    // If couldnt find book then set to default
                    if (book == null && books.Count > 0)
                        book = books[0];
                    // Reset chapter
                    Chapter = Chapter;
                    
                }
            }
        }
        public int Chapter
        {
            get { return chapter; }
            set {
                if (value < 1)
                    chapter = book.Chapters.Count;
                else if (value > book.Chapters.Count)
                    chapter = 1;
                else
                    chapter = value;
                // Reset verse
                Verse = Verse;
            }
        }
        public int Verse
        {
            get { return verse; }
            set {
                if (value < 1)
                    verse = book.Chapters[chapter - 1].Verses.Count;
                else if (value > book.Chapters[chapter - 1].Verses.Count)
                    verse = 1;
                else
                    verse = value;
            }
        }
        public string Position
        {
            get
            {
                if (book != null)
                    return String.Format("{0} {1}:{2}", book.Title, chapter, verse);
                else
                    return null;
            }
        }
        public string PositionContentWav
        {
            get { return book.Chapters[chapter-1].Verses[verse-1].ContentWav; }
        }
        public string PositionNameWav
        {
            get { return book.Chapters[chapter - 1].Verses[verse - 1].NameWav; }
        }

        List<Book> books = new List<Book>();

        public BibleXml(string filename)
        {
            // Create xml reader
            XmlReader reader = XmlReader.Create(filename);
            // vars
            Book currentBook = null;
            Chapter currentChapter = null;
            Verse currentVerse = null;
            string currentNodeName = null;
            // Read xml data into list of book objects
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        currentNodeName = reader.Name;
                        switch (reader.Name)
                        {
                            case "book":
                                currentBook = new Book();
                                currentBook.Title = reader.GetAttribute("title");
                                books.Add(currentBook);
                                break;
                            case "chapter":
                                if (currentBook != null)
                                {
                                    currentChapter = new Chapter();
                                    currentBook.Chapters.Add(currentChapter);
                                }
                                break;
                            case "verse":
                                if (currentChapter != null)
                                {
                                    currentVerse = new Verse();
                                    currentChapter.Verses.Add(currentVerse);
                                }
                                break;
                        }
                        break;
                    case XmlNodeType.Text:
                        switch (currentNodeName)
                        {
                            case "name":
                                if (currentVerse != null)
                                    currentVerse.NameWav = reader.Value;
                                break;
                            case "content":
                                if (currentVerse != null)
                                    currentVerse.ContentWav = reader.Value;
                                break;
                        }
                        break;
                }
            }
        }

        public void PrevBook()
        {
            for (int i = 0; i < books.Count; i++)
                if (books[i] == book)
                {
                    if (i == 0)
                        book = books[books.Count - 1];
                    else
                        book = books[i - 1];
                    return;
                }
        }

        public void NextBook()
        {
            for (int i = 0; i < books.Count; i++)
                if (books[i] == book)
                {
                    if (i == books.Count-1)
                        book = books[0];
                    else
                        book = books[i + 1];
                    return;
                }
        }

        public void PrevChapter()
        {
            Chapter -= 1;
        }

        public void NextChapter()
        {
            Chapter += 1;
        }

        public void PrevVerse()
        {
            Verse -= 1;
        }

        public void NextVerse()
        {
            Verse += 1;
        }
    }
}
